﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.IO;

namespace NJL.WebManage.Orders
{
    public partial class OrderDetails : BasePage
    {
        private readonly NJL.BLL.NJL_BOOK_ORDER bllBookOrder = new BLL.NJL_BOOK_ORDER();
        private readonly NJL.BLL.NJL_BOOK bllBook = new BLL.NJL_BOOK();
        private readonly NJL.BLL.NJL_CS_BOOK_PAY bllBookPay = new BLL.NJL_CS_BOOK_PAY();
        private readonly NJL.BLL.NJL_ATTRACTION bllAttr = new BLL.NJL_ATTRACTION();
        private readonly NJL.BLL.NJL_COMPANY bllCompany = new BLL.NJL_COMPANY();
        protected NJL.Model.NJL_BOOK_ORDER modelBookOrder;
        protected NJL.Model.NJL_BOOK modelBook;
        NJL.Model.NJL_CS_BOOK_PAY modelPay;
        protected NJL.Model.NJL_ATTRACTION modelAttr;
        protected NJL.Model.NJL_COMPANY modelCompany;
        NJL.BLL.NJL_SYS_BASE bllSysBase = new BLL.NJL_SYS_BASE();
        NJL.Model.NJL_SYS_BASE modelSysBase;

        protected void Page_Load(object sender, EventArgs e)
        {

            modelBookOrder = bllBookOrder.GetModel(Request.QueryString["id"]);
            litOrderNumber.Text = modelBookOrder.ORDER_NO;                        //订单号
            litTicketNumber.Text = modelBookOrder.TICKET_COUNT.ToString();  //门票数量
            litTotlePrice.Text = string.Format("{0:0.00}", modelBookOrder.ORDER_TOTAL); //总价格
            litVISIT_DATE.Text = string.Format("{0:yyyy-MM-dd}", modelBookOrder.VISIT_DATE);  // 游玩时间
            litOrderCreatDate.Text = modelBookOrder.CREATE_DATE_TIME.ToString();       //下单时间
            litAddress.Text = "景区售票处";

            modelBook = bllBook.GetModel(modelBookOrder.NJL_BOOK_ID);
            litBookName.Text = modelBook.BOOK_NAME;                              //预订类型
            litBackPrice.Text = string.Format("{0:0.00}", modelBook.BACK_PRICE); //返点金额

            litPickName.Text = modelBookOrder.PICK_NAME;                          //取票人姓名
            litPickPhone.Text = modelBookOrder.PICK_MOBILE;                       //取票人电话
            litPickEmail.Text = modelBookOrder.PICK_EMAIL;                        //取票人邮箱
            litydrxm.Text = modelBookOrder.BOOK_NAME;                             //预订人姓名
            litydrsj.Text = modelBookOrder.BOOK_MOBILE;                           //预订人手机
            litydryx.Text = modelBookOrder.BOOK_EMAIL;                            //预订人邮箱
            if (modelBookOrder.IS_REGISTER_USER == 1)
            {
                litUserType.Text = "注册用户";
            }
            else
            {
                litUserType.Text = "非注册用户";
            }

            litSMSCount.Text = modelBookOrder.SEND_SMS_COUNT.ToString();
            litSendSMSDate.Text = modelBookOrder.SEND_SMS_LAST_DATE_TIME.ToString();
            litPrice.Text = string.Format("{0:0.00}", modelBookOrder.BOOK_PRICE);                            //单价
            modelPay = bllBookPay.GetModel(modelBook.NJL_CS_BOOK_PAY_TYPE_CODE);
            litPayment.Text = modelPay.TYPE_NAME;                                 //支付方式
            if (modelBook.NJL_CS_BOOK_PAY_TYPE_CODE == "2")
            {
                if (modelBookOrder.ORDER_STATE.ToString() == "1")
                {
                    litOrderState.Text = "预订成功";
                }
            }
            if (modelBook.NJL_CS_BOOK_PAY_TYPE_CODE == "1")
            {
                if (modelBookOrder.ORDER_STATE== 1)
                {
                    litOrderState.Text = "待支付状态";
                }
                else if (modelBookOrder.ORDER_STATE == 2)
                {
                    litOrderState.Text = "支付成功";
                }
            }
            if (modelBook.TYPE_CODE == "1")
            {
                modelAttr = bllAttr.GetModel(modelBook.TARGET_ID);
                litAttrName.Text = modelAttr.ATTRACTION_NAME;
                //litAttrName.Text = string.Format("<a href=\"http://{0}/{1}/jdms/{2}.html\" target=\"_blank\">{3}</a>", modelSysBase.WEBSITE_DOMAIN, modelCompany.ID, modelAttr.ATTRACTION_NAME);
                litjdnjl.Text = "景点";
            }
            else if (modelBook.TYPE_CODE == "2")
            {
                modelCompany = bllCompany.GetModel(modelBook.TARGET_ID);
                modelSysBase = bllSysBase.GetModel(modelBook.NJL_SYS_BASE_ID);
                //litAttrName.Text = modelCompany.COMPANY_NAME;
                litAttrName.Text = string.Format("<a href=\"http://{0}/shangpu/{1}-index.html\" target=\"_blank\">{2}</a>", modelSysBase.WEBSITE_DOMAIN, modelCompany.ID, modelCompany.COMPANY_NAME);
                litjdnjl.Text = "农家乐、山庄";
            }



        }

        protected void btnSendSMS_Click(object sender, EventArgs e)
        {

            //如果有接收短信的手机号码，则发送给商户的短信接收号码
            string strMobileHost = System.Configuration.ConfigurationManager.AppSettings["CompanySmsMobile"];
            if (modelCompany.RECEIVE_SMS_MOBILE != null && modelCompany.RECEIVE_SMS_MOBILE != "")
            {
                strMobileHost = modelCompany.RECEIVE_SMS_MOBILE;
            }

            string strWebsiteName = WEBSITE_NAME;
            string strCompanyNameSms = modelCompany.COMPANY_NAME_SMS;
            string strPlayDate = modelBookOrder.VISIT_DATE.Value.ToString("MM月dd日");
            string strTitcketType = modelBook.BOOK_NAME;
            string strTitcketCount = modelBookOrder.TICKET_COUNT.Value.ToString();
            string strTotal = modelBookOrder.ORDER_TOTAL.Value.ToString("#");
            string strBookPrice = modelBookOrder.BOOK_PRICE.Value.ToString("#");
            string strOrderNo = modelBookOrder.ORDER_NO;
            string strPayType = modelBookOrder.TYPE_CODE == "2" ? "付款" : "直接";

            string strPickName = modelBookOrder.PICK_NAME;
            string strPickMobile = modelBookOrder.PICK_MOBILE;

            string strBookName = modelBookOrder.BOOK_NAME;
            string strBookMobile = modelBookOrder.BOOK_MOBILE;
            //取票地点
            string strPickTicketAddress = modelBook.PICK_TICKET_ADDRESS;

            string strContentToPickerTemplateSelf = GetSmsTemplate("C01");
            string strContentToPickerTemplateOther = GetSmsTemplate("C02");
            string strContentToCompanyTemplate = GetSmsTemplate("C03");

            string SMSContentToPicker;
            string SMSContentToCompany;

            //发给商户的短信
            strContentToCompanyTemplate = strContentToCompanyTemplate.Replace("{$WebsiteName$}", strWebsiteName);
            strContentToCompanyTemplate = strContentToCompanyTemplate.Replace("{$PickName$}", strPickName);
            strContentToCompanyTemplate = strContentToCompanyTemplate.Replace("{$PickMobile$}", strPickMobile);
            strContentToCompanyTemplate = strContentToCompanyTemplate.Replace("{$CompanyNameSms$}", strCompanyNameSms);
            strContentToCompanyTemplate = strContentToCompanyTemplate.Replace("{$PlayDate$}", strPlayDate);
            strContentToCompanyTemplate = strContentToCompanyTemplate.Replace("{$TitcketType$}", strTitcketType);
            strContentToCompanyTemplate = strContentToCompanyTemplate.Replace("{$TitcketCount$}", strTitcketCount);
            //strContentToCompanyTemplate = strContentToCompanyTemplate.Replace("{$Total$}", strTotal);
            strContentToCompanyTemplate = strContentToCompanyTemplate.Replace("{$BookPrice$}", strBookPrice);//预定单价
            strContentToCompanyTemplate = strContentToCompanyTemplate.Replace("{$OrderNo$}", strOrderNo);
            strContentToCompanyTemplate = strContentToCompanyTemplate.Replace("{$PayType$}", strPayType);
            SMSContentToCompany = strContentToCompanyTemplate;

            if (modelBookOrder.PICK_MOBILE != modelBookOrder.BOOK_MOBILE)
            {
                strContentToPickerTemplateOther = strContentToPickerTemplateOther.Replace("{$BookName$}", strBookName);
                strContentToPickerTemplateOther = strContentToPickerTemplateOther.Replace("{$BookMobile$}", strBookMobile);
                strContentToPickerTemplateOther = strContentToPickerTemplateOther.Replace("{$CompanyNameSms$}", strCompanyNameSms);
                strContentToPickerTemplateOther = strContentToPickerTemplateOther.Replace("{$PlayDate$}", strPlayDate);
                strContentToPickerTemplateOther = strContentToPickerTemplateOther.Replace("{$TitcketType$}", strTitcketType);
                strContentToPickerTemplateOther = strContentToPickerTemplateOther.Replace("{$TitcketCount$}", strTitcketCount);
                //strContentToPickerTemplateOther = strContentToPickerTemplateOther.Replace("{$Total$}", strTotal);
                strContentToPickerTemplateOther = strContentToPickerTemplateOther.Replace("{$BookPrice$}", strBookPrice);//预定单价
                strContentToPickerTemplateOther = strContentToPickerTemplateOther.Replace("{$OrderNo$}", strOrderNo);
                strContentToPickerTemplateOther = strContentToPickerTemplateOther.Replace("{$PayType$}", strPayType);
                strContentToPickerTemplateOther = strContentToPickerTemplateOther.Replace("{$WebsiteName$}", strWebsiteName);
                strContentToPickerTemplateOther = strContentToPickerTemplateOther.Replace("{$CompanyContactMobile$}", strMobileHost);
                //增加取票地点
                strContentToPickerTemplateOther = strContentToPickerTemplateOther.Replace("{$PickTicketAddress$}", strPickTicketAddress);
                SMSContentToPicker = strContentToPickerTemplateOther;
            }
            else
            {
                strContentToPickerTemplateSelf = strContentToPickerTemplateSelf.Replace("{$CompanyNameSms$}", strCompanyNameSms);
                strContentToPickerTemplateSelf = strContentToPickerTemplateSelf.Replace("{$PlayDate$}", strPlayDate);
                strContentToPickerTemplateSelf = strContentToPickerTemplateSelf.Replace("{$TitcketType$}", strTitcketType);
                strContentToPickerTemplateSelf = strContentToPickerTemplateSelf.Replace("{$TitcketCount$}", strTitcketCount);
                //strContentToPickerTemplateSelf = strContentToPickerTemplateSelf.Replace("{$Total$}", strTotal);
                strContentToPickerTemplateSelf = strContentToPickerTemplateSelf.Replace("{$BookPrice$}", strBookPrice);//预定单价
                strContentToPickerTemplateSelf = strContentToPickerTemplateSelf.Replace("{$OrderNo$}", strOrderNo);
                strContentToPickerTemplateSelf = strContentToPickerTemplateSelf.Replace("{$PayType$}", strPayType);
                strContentToPickerTemplateSelf = strContentToPickerTemplateSelf.Replace("{$WebsiteName$}", strWebsiteName);
                strContentToPickerTemplateSelf = strContentToPickerTemplateSelf.Replace("{$CompanyContactMobile$}", strMobileHost);
                //增加取票地点
                strContentToPickerTemplateSelf = strContentToPickerTemplateSelf.Replace("{$PickTicketAddress$}", strPickTicketAddress);
                SMSContentToPicker = strContentToPickerTemplateSelf;
            }

            if (modelBookOrder.PICK_MOBILE.Length == 11)
            {
                string i = base.SendSMS(modelBookOrder.PICK_MOBILE, SMSContentToPicker);

                //i = base.SendSMS(strMobileHost, SMSContentToCompany);
                if (!i.StartsWith("-"))
                {
                    modelBookOrder.SEND_SMS_COUNT += 1;
                    modelBookOrder.SEND_SMS_LAST_DATE_TIME = DateTime.Now;
                    bllBookOrder.Update(modelBookOrder);
                    Royalsoft.Common.MessageBox.Show(this, "短信发送成功");
                }
                else
                {
                    Royalsoft.Common.MessageBox.Show(this, "发送失败:"+i);
                }
            }

            if (modelBookOrder.BOOK_EMAIL != null && modelBookOrder.BOOK_EMAIL != "")
            {
                //发送邮件
            }

        }

        private string GetSmsTemplate(string TEMPLATE_TYPE)
        {
            Encoding code = Encoding.GetEncoding("UTF-8");
            // 读取模板文件 
            string temp = "";
            if (TEMPLATE_TYPE == "C01")
            {
                temp = HttpContext.Current.Server.MapPath("~/SMSTemplate/SMS_Book_Company_01.txt");
            }
            if (TEMPLATE_TYPE == "C02")
            {
                temp = HttpContext.Current.Server.MapPath("~/SMSTemplate/SMS_Book_Company_02.txt");
            }
            if (TEMPLATE_TYPE == "C03")
            {
                temp = HttpContext.Current.Server.MapPath("~/SMSTemplate/SMS_Book_Company_03.txt");
            }
            StreamReader sr = null;
            //StreamWriter sw = null;
            string str = "";
            try
            {
                sr = new StreamReader(temp, code);
                str = sr.ReadToEnd(); // 读取文件 
            }
            catch (Exception exp)
            {
                HttpContext.Current.Response.Write(exp.Message);
                HttpContext.Current.Response.End();
                sr.Close();
            }
            return str;
        }
    }
}